<?php

/**
 * Division models
 *
 * Division models in data
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */

/**
 * Class Division
 *
 * Class Division in data models
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Data_Department_Division extends Eloquent
{

    public static $timestamps = true;
    public static $table = 'sys_user_bahagian';
    public static $key = 'sub_no_auto';
    public static $rules = array(
                                'sub_bahagian_name' => 'required|Unique:sys_user_bahagian',
                            );

	/**
     * Function validate that pass parameter
     *
     * @param varchar $data the varchar to quote
	 *
	 * @return parameter.
     */
    public static function validate($data)
	{
        return Validator::make($data, Static::$rules);
    }

	/**
     * Function listDivision
	 *
	 * @return listDivision.
     */
    public static function listDivision()
	{
        $allData = Data_Department_Division::where('suj_no_auto', '=', URI::segment(5))
			->order_by('sub_bahagian_name','ASC')
			->paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
        $datagrid->setFields(array('sub_bahagian_name' => Str::upper(Lang::line('admin.division')->get())));
		$datagrid->setFields(array('sub_bahagian_desc' => Str::upper(Lang::line('global.desc')->get())));
        $datagrid->setAction(Lang::line('admin.unit')->get(), url('administrator/organization/unit/'.URI::segment(5)), false);
        $datagrid->setAction(Lang::line('global.edit')->get(), 'editDiv', true, array('sub_no_auto'));
        $datagrid->setAction(Lang::line('global.delete')->get(), 'deleteDiv', true, array('sub_no_auto', 'sub_bahagian_name'));
        $datagrid->setTable('wt_par_bahagian', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->showNumbering(true);
        $datagrid->build($allData, static::$key);

        return $datagrid->render();
    }
	
	/**
     * Function addDivision that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function addDivision($input)
	{
        if ($input['wpb_no_auto'] == null) {
            $division = new Data_Department_Division;
            $action = Lang::line('global.add')->get();
            $division->sub_cipta_oleh = Auth::user()->su_id;
        } else {
            $division = Data_Department_Division::find($input['wpb_no_auto']);
            $action = Lang::line('global.edit')->get();
        }
        
        $division->sub_bahagian_name = Str::title($input['wpb_bahagian_name']);
        $division->sub_bahagian_desc = $input['wpb_bahagian_desc'];
        $division->suj_no_auto = $input['wpj_no_auto'];
        $division->sub_kemaskini_oleh = Auth::user()->su_id;
        $division->save();

        Log::write(
			Lang::line('admin.division')->get(), 
			$action.' <strong>'.$input['wpb_bahagian_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.division')->get(), 
			Auth::user()->su_id, 
			$action.' <strong>'.$input['wpb_bahagian_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }
	
	public static function editDivision($input)
	{
        $division = Data_Department_Division::find($input['wpb_no_auto']);
        $action = Lang::line('global.edit')->get();
        
        $division->sub_bahagian_name = Str::title($input['wpb_bahagian_name']);
        $division->sub_bahagian_desc = $input['wpb_bahagian_desc'];
        $division->suj_no_auto = $input['wpj_no_auto'];
        $division->sub_kemaskini_oleh = Auth::user()->su_id;
        $division->save();

        Log::write(
			Lang::line('admin.division')->get(), 
			$action.' <strong>'.$input['wpb_bahagian_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.division')->get(), 
			Auth::user()->su_id, 
			$action.' <strong>'.$input['wpb_bahagian_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }

	/**
     * Function divInfo that pass parameter
     *
     * @param integer $id the integer to quote
	 *
	 * @return parameter.
     */
    public static function divInfo($id)
	{
        $data = array();
        $raw = Data_Department_Division::find($id);
        $data['wpb_no_auto'] = $raw->sub_no_auto;
        $data['wpb_bahagian_name'] = $raw->sub_bahagian_name;
        $data['wpb_bahagian_desc'] = $raw->sub_bahagian_desc;
        $data['wpj_no_auto'] = $raw->suj_no_auto;

        return $data;
    }
	
	/**
     * Function arrayDiv that pass parameter
     *
     * @param varchar $current the varchar to quote
	 *
	 * @return parameter.
     */
    public static function arrayDiv($current)
	{
        $bahagian = Data_Department_Division::where('suj_no_auto', '=', $current)->get(array('sub_no_auto', 'sub_bahagian_name')) ;

        $arrayDiv = array('Sila Pilih');

        foreach ($bahagian as $value) {
            $arrayDiv[$value->sub_no_auto] = $value->sub_bahagian_name;
        }

        return $arrayDiv;
    }
	
	/**
     * Function deleteDiv that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function deleteDiv($input)
	{
        Data_Department_Division::find($input['id'])->delete();

        Log::write(
			Lang::line('admin.division')->get(), 
			Lang::line('global.delete')->get().' <strong>'.$input['name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.division')->get(), 
			Auth::user()->su_id, 
			Lang::line('global.delete')->get().' <strong>'.$input['name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }
	 
	/**
     * Custom Data Validation
     *
     * @param integer $id the integer to quote
	 *
	 * @return void.
     */
    public static function validateData($id)
	{
        $bil = Console_Profile::where('sub_no_auto', '=', $id)->count();
        $bil = $bil + Data_Department_Unit::where('sub_no_auto', '=', $id)->count();

        if ($bil > 0) {
            return false;
        } else {
            return true;
        }
    }

}

?>